package org.example;

import cn.hutool.core.io.FileUtil;
import com.google.common.base.Throwables;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;

import java.io.File;
import java.io.IOException;

/**
 * Author:dodayum
 * Date:2025-11-04
 */
public class UnZip {

    public static void main(String[] args) {
        String zipFilePath = "/Users/dodayum/Downloads/AiDSC-V2.0R25C00SPC200B031-X86-install.zip";      // ZIP 文件路径
        String password = "your_password";                 // ZIP 密码
        String destDir = "/Users/dodayum/Downloads/extract_result";  // 解压目标目录

        File zipFile = FileUtil.file(zipFilePath);
        File outputDir = FileUtil.mkdir(destDir);


        unzipWithPassword(zipFile,outputDir,password);
    }

    public static void unzipWithPassword(File zipFile, File outputDir, String password) {

        try (ZipFile zip = new ZipFile(zipFile)){

            // 设置解压缩密码
            if (zip.isEncrypted()) {
                zip.setPassword(password.toCharArray());
            }
            zip.extractAll(outputDir.getAbsolutePath()); // 报错点

            System.out.println("✅ 解压完成：" + outputDir.getAbsolutePath()+"\n正确密码："+password);
        } catch (ZipException e) {
            System.err.println("❌ 解压失败：" + e.getMessage()+"\t密码："+password);
            System.out.println(Throwables.getStackTraceAsString(e));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }




    private static final char[] CHARS = (
            "abcdefghijklmnopqrstuvwxyz" +   // 小写字母
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +   // 大写字母
                    "0123456789" +                   // 数字
                    "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? " // 符号 + 空格
    ).toCharArray();


    private static void buildPassword(){
        for (int length = 5; length <= 10; length++) {
            System.out.println("=== 正在生成长度为 " + length + " 的组合 ===");
            generate("", length);
        }
    }

    private static void generate(String prefix, int remaining) {
        if (remaining == 0) {
            System.out.println(prefix);


            return;
        }

        for (char c : CHARS) {
            generate(prefix + c, remaining - 1);
        }
    }

}
